package com.geek.factory.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.dto.ProductionPlanDTO;
import com.geek.factory.entity.ProductionPlan;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface ProductionPlanMapper  extends BaseMapper<ProductionPlan> {

    @Select("SELECT pp.id, pp.plan_code, pp.product_id, p.name AS product_name, " +
            "pp.product_line_id, pl.line_name AS product_line_name, pp.quantity, " +
            "pp.plan_start_time, pp.complete_time, pp.status, pp.create_time, pp.is_in_stock " +
            "FROM production_plan pp " +
            "LEFT JOIN product p ON pp.product_id = p.id " +
            "LEFT JOIN product_line pl ON pp.product_line_id = pl.id " +
            "WHERE (#{planCode} IS NULL OR pp.plan_code LIKE CONCAT('%', #{planCode}, '%')) " +
            "AND (#{productId} IS NULL OR pp.product_id = #{productId}) " +
            "AND (#{productLineId} IS NULL OR pp.product_line_id = #{productLineId}) " +
            "AND (#{status} IS NULL OR pp.status = #{status}) " +
            "ORDER BY pp.create_time DESC " +
            "LIMIT #{offset}, #{size}")
    List<ProductionPlanDTO> getPlanListWithNamesPaged(
            @Param("planCode") String planCode,
            @Param("productId") Long productId,
            @Param("productLineId") String productLineId,
            @Param("status") String status,
            @Param("offset") int offset,
            @Param("size") int size
    );
    /**
     * 查询总记录数（条件与分页一致）
     */
    @Select("SELECT COUNT(1) " +
            "FROM production_plan pp " +
            "LEFT JOIN product p ON pp.product_id = p.id " +
            "LEFT JOIN product_line pl ON pp.product_line_id = pl.id " +
            "WHERE (#{planCode} IS NULL OR pp.plan_code LIKE CONCAT('%', #{planCode}, '%')) " +
            "AND (#{productId} IS NULL OR pp.product_id = #{productId}) " +
            "AND (#{productLineId} IS NULL OR pp.product_line_id = #{productLineId}) " +
            "AND (#{status} IS NULL OR pp.status = #{status})")
    long countPlanListWithNames(
            @Param("planCode") String planCode,
            @Param("productId") Long productId,
            @Param("productLineId") String productLineId,
            @Param("status") String status
    );

    @Select("SELECT pp.*, " +
            "p.name AS product_name, " +
            "pl.line_name AS product_line_name " +
            "FROM production_plan pp " +
            "LEFT JOIN product p ON pp.product_id = p.id " +
            "LEFT JOIN product_line pl ON pp.product_line_id = pl.id " +
            "WHERE pp.id = #{id}")
    ProductionPlanDTO getPlanDetailById(@Param("id") Long id);


    /**
     * 更新计划状态
     * @param planId 计划ID
     * @param status 状态值（"0"：未开始，"1"：进行中，"2"：已完成）
     */
    @Update("UPDATE production_plan SET status = #{status} WHERE id = #{planId}")
    void updatePlanStatus(@Param("planId") Long planId, @Param("status") String status);

    @Select("SELECT id FROM production_plan WHERE plan_code = #{planCode}")
    Long getPlanIdByPlanCode(@Param("planCode") String planCode);

    @Select("SELECT * FROM production_plan WHERE id = #{id}")
    ProductionPlan getById(@Param("id") Long id);

    @Select("SELECT plan_code FROM production_plan WHERE id = #{id}")
    String getPlanCodeById(@Param("id") Long id);


}
